package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateArticleCategory
//@description: 创建ArticleCategory记录
//@param: articleCategory model.ArticleCategory
//@return: err error

func CreateArticleCategory(articleCategory model.ArticleCategory) (err error) {
	err = global.GVA_DB.Create(&articleCategory).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteArticleCategory
//@description: 删除ArticleCategory记录
//@param: articleCategory model.ArticleCategory
//@return: err error

func DeleteArticleCategory(articleCategory model.ArticleCategory) (err error) {
	err = global.GVA_DB.Delete(&articleCategory).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteArticleCategoryByIds
//@description: 批量删除ArticleCategory记录
//@param: ids request.IdsReq
//@return: err error

func DeleteArticleCategoryByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.ArticleCategory{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateArticleCategory
//@description: 更新ArticleCategory记录
//@param: articleCategory *model.ArticleCategory
//@return: err error

func UpdateArticleCategory(articleCategory model.ArticleCategory) (err error) {
	err = global.GVA_DB.Save(&articleCategory).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetArticleCategory
//@description: 根据id获取ArticleCategory记录
//@param: id uint
//@return: err error, articleCategory model.ArticleCategory

func GetArticleCategory(id uint) (err error, articleCategory model.ArticleCategory) {
	err = global.GVA_DB.Where("id = ?", id).First(&articleCategory).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetArticleCategoryInfoList
//@description: 分页获取ArticleCategory记录
//@param: info request.ArticleCategorySearch
//@return: err error, list interface{}, total int64

func GetArticleCategoryInfoList(info request.ArticleCategorySearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.ArticleCategory{})
    var articleCategorys []model.ArticleCategory
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&articleCategorys).Error
	return err, articleCategorys, total
}